package com.load.utils;

import java.io.*;

import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.io.SVNRepository;

import com.database.objects.File;
import com.load.exception.*;

public class FileUtil {

	public static ByteArrayOutputStream obtainContent(SVNRepository svnRepository, File file) throws DelFileException, DirectoryException {
		if (file.isDeleteStatus()) {
			throw new DelFileException("The file does not exists in this version");
		} else {
			try {
				SVNNodeKind nodeKind = svnRepository.checkPath(file.getPath(), file.getNumberCommit());
				if (nodeKind.equals(SVNNodeKind.FILE)) {
					SVNProperties fileProperties = new SVNProperties();
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					String path = file.getPath();
					Long commitNumber = file.getNumberCommit();
					svnRepository.getFile(path, commitNumber, fileProperties, baos);

					baos.close();
					return baos;
				}
				throw new DirectoryException("The file is a directory");
			} catch (SVNException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static boolean pathIsFile(String path) {
		return path.contains(".");
	}

	public static boolean pathIsFileOO(String path) {
		return path.endsWith(".java");
	}

}